<?php
/*********************************************
  Dragonfly CMS, Copyright (c) 2004 by CPGNuke Dev Team
  http://dragonflycms.org
  Released under GNU GPL version 2 or any later version
**********************************************/

class Dragonfly_Page {

	static private
		# head
		$title,
		$metaTags = array(),
		$data = array(),
		# body
		$sysMessage = array(),
		#footer
		$signature = array();


	/*
	 * Header
	 */

	# overwrite or add to title using '.' as very first char
	static public function setTitle($str)
	{
		if (empty($str)) {
			trigger_error('Cannot set an empty title.', E_USER_WARNING);
			return;
		}
		if ('.' === $str[0]) {
			$str = substr($str, 1);
			$str = self::$title .= $str;
		}
		self::$title = strip_tags($str);
	}

	# meta tags
	static public function metatag($name, $content, $rename='')
	{
		if (empty($content)) return;
		$content = htmlprepare(Dragonfly_Output_Xhtml::minify($content), false, ENT_QUOTES, true);
		self::$metaTags[$name]['name'] = $rename ? $rename : 'name';
		self::$metaTags[$name]['content'] = $content;
	}

	# link tags
	static public function link($rel, $href)
	{
		self::$data[] = '<link rel="'.$rel.'" href="'.$href.'"/>';
	}

	# any custom tag
	static public function tag($str)
	{
		self::$data[] = '<'.strip_tags($str).'/>';
	}

	# custom data
	static public function headerData($str)
	{
		self::$data[] = $str;
	}

	# create keywords from context, $tags will prepend custom keywords
	# $context string
	# $tags mixed
	# ($context) only elaborate the string
	# ($context, $tags) elaborate the string and increase the weight of each found $tag
	static public function createKeywords($context, $tags=null)
	{
		$context = \Poodle\Unicode::as_search_txt($context);
		$context = array_count_values(array_map('mb_strtolower', explode(' ', $context)));
		foreach ($context as $k => $v) {
			if (!isset($k[2])) unset($context[$k]);
		}

		if ($tags)
		{
			if (is_string($tags)) {
				$tags = array_map('trim', explode(',', $tags));
			}
			if (is_array($tags)) {
				$tags = array_map('mb_strtolower', array_map('strip_tags', $tags));
				foreach ($tags as $v) {
					if (isset($context[$v])) $context[$v] += 3; // $CFG
				}
			}
		}
		arsort($context);
		$context = array_keys(array_slice($context, 0, 15)); // $CFG
		self::metatag('keywords', htmlprepare(implode(',', $context)));
	}

	# retrive it all
	static public function getHeaders()
	{
		global $header, $modheader;
		foreach (self::$metaTags as $k => $v) {
			self::$data[] = '<meta '.$v['name'].'="'.$k.'" content="'.$v['content'].'"/>';
		}
		if (DF_MODE_DEVELOPER) self::$data[] = '<!-- still using $header -->';
		self::$data[] = $header;
		if (DF_MODE_DEVELOPER) self::$data[] = '<!-- still using $modheader -->';
		self::$data[] = $modheader;
		if (DF_MODE_DEVELOPER) self::$data[] = '<!-- end -->';
		return Dragonfly_Output_Xhtml::minify(implode("\n", self::$data)."\n");
	}

	# access private methods read only
	static public function get($k)
	{
		if ('title' === $k) return self::$title;
		if (isset(self::$metaTags[$k])) return self::$metaTags[$k]['content'];
	}

	/*
	 * Body
	 */

	/*
	 * Footer
	 */

}
